home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: mxsld2.pd.infn.it!LORETI
- From: loreti@mxsld2.pd.infn.it (Maurizio Loreti)
- Subject: Re: What to do when feof() is NOT feof()
- X-Nntp-Posting-Host: mxsld2.pd.infn.it
- Message-ID: <Dn7x8s.53s@news.cern.ch>
- Sender: news@news.cern.ch (USENET News System)
- Reply-To: loreti@mxsld2.pd.infn.it
- Organization: I.N.F.N. Padova - CDF/CMS VAXcluster
- References: <824554814snz@metsys.demon.co.uk>
- Date: Fri, 23 Feb 1996 07:38:50 GMT
-
- In article <824554814snz@metsys.demon.co.uk>, John Standen <John@metsys.demon.co.uk> writes:
- >I have a function in a program which is reading each character
- >in a file of some 700k characters.
- >
- >The do{ while !feof(in) sometimes causes the function to terminate
- >very early in the file.
-
- Hmmm, you did not notice, reading the FAQ list before posting, Q/A
- 12.2:
-
- 12.2: Why does the code
-
- while(!feof(infp)) {
- fgets(buf, MAXLINE, infp);
- fputs(buf, outfp);
- }
-
- copy the last line twice?
-
- A: In C, EOF is only indicated *after* an input routine has tried
- to read, and has reached end-of-file. (In other words, C's I/O
- is not like Pascal's.) Usually, you should just check the
- return value of the input routine (fgets() in this case); often,
- you don't need to use feof() at all.
-
- References: K&R2 Sec. 7.6 p. 164; ANSI Sec. 4.9.3, Sec. 4.9.7.1,
- Sec. 4.9.10.2; ISO Sec. 7.9.3, Sec. 7.9.7.1, Sec. 7.9.10.2; H&S
- Sec. 15.14 p. 382.
-
- 12.2 states that your approach to the file is wrong; however the error
- should appear at the end of the file, not in the middle. Are you sure
- your file does not contain any control characters?
- --
- Maurizio Loreti http://mvxpd5.pd.infn.it/wwwcdf/mlo.html
- Un. of Padova, Dept. of Physics - Padova, Italy loreti@padova.infn.it
-